package sample4.provider.ddl;

import android.text.TextUtils;

/**
 * Create view...
 */
public class SqliteViewBuilder extends AbsDdlBuilder {
    private String mFrom;
    private String mWhere;
    private String mGroupBy;
    private String mHaving;
    private String mOrderBy;

    /**
     * コンストラクタ
     * 
     * @param name ビュー名
     */
    public SqliteViewBuilder(String name) {
        super(name);
    }

    /**
     * selectパートを追加する
     * 
     * @param columnAlias
     * @param selectPart
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder addSelectPart(String columnAlias, String selectPart) {
        addColumn(selectPart + " AS " + columnAlias);
        return this;
    }

    /**
     * selectパートを追加する
     * 
     * @param columnAlias
     * @param selectPartFormat
     * @param text
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder addSelect(String columnAlias, String selectPartFormat, String... text) {
        return addSelectPart(columnAlias, String.format(selectPartFormat, text));
    }

    /**
     * fromパートを設定する
     * 
     * @param from データの元先
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder setFrom(String from) {
        this.mFrom = from;
        return this;
    }

    /**
     * fromパートを設定する
     * 
     * @param fromPartFormat
     * @param text
     * @return　SqliteViewBuilder
     */
    public SqliteViewBuilder setFrom(String fromPartFormat, String... text) {
        return setFrom(String.format(fromPartFormat, text));
    }

    /**
     * whereパートを設定する
     * 
     * @param where
     * @return　SqliteViewBuilder
     */
    public SqliteViewBuilder setWhere(String where) {
        this.mWhere = where;
        return this;
    }

    /**
     * whereパートを設定する
     * 
     * @param wherePartFormat
     * @param text
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder setWhere(String wherePartFormat, String... text) {
        return setWhere(String.format(wherePartFormat, text));
    }

    /**
     * group byパートを設定する
     * 
     * @param groupBy
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder setGroupBy(String groupBy) {
        this.mGroupBy = groupBy;
        return this;
    }

    /**
     * group byパートを設定する
     * 
     * @param groupByPartFormat
     * @param text
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder setGroupBy(String groupByPartFormat, String... text) {
        return setGroupBy(String.format(groupByPartFormat, text));
    }

    /**
     * order byパートを設定する
     * 
     * @param orderBy
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder setOrderBy(String orderBy) {
        this.mOrderBy = orderBy;
        return this;
    }

    /**
     * order byパートを設定する
     * 
     * @param orderByPartFormat
     * @param text
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder setOrderBy(String orderByPartFormat, String... text) {
        return setOrderBy(String.format(orderByPartFormat, text));
    }

    /**
     * havingパートを設定する
     * 
     * @param having
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder setHaving(String having) {
        this.mHaving = having;
        return this;
    }

    /**
     * havingパートを設定する
     * 
     * @param havingByPartFormat
     * @param text
     * @return SqliteViewBuilder
     */
    public SqliteViewBuilder setHaving(String havingByPartFormat, String... text) {
        return setHaving(String.format(havingByPartFormat, text));
    }

    @Override
    String toDropSql() {
        return "DROP VIEW IF EXISTS " + getName();
    }

    @Override
    String toCreateSql() {
        StringBuilder sql = new StringBuilder();
        // start
        sql.append("CREATE VIEW INDEX IF NOT EXISTS ").append(getName()).append(" AS").append('\n');
        // select
        sql.append("SELECT ");
        boolean addComma = false;
        for (String part : getColumn()) {
            if (addComma) {
                sql.append('\n').append("      ,");
            }
            addComma = true;
            sql.append(part);
        }
        // from
        if (!TextUtils.isEmpty(mFrom)) {
            sql.append('\n').append("  FROM ").append(mFrom);
        }

        if (!TextUtils.isEmpty(mWhere)) {
            sql.append('\n').append(" WHERE ").append(mWhere);
        }

        if (!TextUtils.isEmpty(mGroupBy)) {
            sql.append('\n').append(" GROUP BY ").append(mGroupBy);
        }

        if (!TextUtils.isEmpty(mHaving)) {
            sql.append('\n').append("HAVING ").append(mHaving);
        }

        if (!TextUtils.isEmpty(mOrderBy)) {
            sql.append('\n').append(" ORDER BY ").append(mOrderBy);
        }
        return sql.toString();
    }
}
